﻿using System;
using System.Windows.Data;

namespace AnthStat.WPF.Applications.Converters
{
    /// <summary>
    /// Converts a datetime value into the number of days elapsed between it and the current datetime in whole days; optionally, it can place this value into a string
    /// </summary>
    public sealed class DaysAgoConverter : IValueConverter
    {
        /// <summary>
        /// Converts the date/time into the number of whole days elapsed between that date and today's date, represented as a string.
        /// </summary>
        /// <param name="value">The date/time to use for the day calculation</param>
        /// <param name="targetType">n/a</param>
        /// <param name="parameter">An optional string that can be used, e.g. '{0} days past', to format the output</param>
        /// <param name="culture">n/a</param>
        /// <returns>string</returns>
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            // TODO: Code contracts

            int days = -1;
            DateTime pastDate = (DateTime)value;

            TimeSpan ts = DateTime.Today - pastDate;

            days = System.Convert.ToInt32(Math.Round(ts.TotalDays, 0));

            string rawMessage = parameter.ToString();

            return String.Format(rawMessage, days.ToString());
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}
